package com.yjd.back.settlement;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.PagerHtmlModel;
import com.yjd.comm.enums.FundsVolumeEnums;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.fundservice.enums.FundChannelEnum;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.org.service.IOrganizationService;
import com.yjd.comm.settlement.model.ClearDayDetailModel;
import com.yjd.comm.settlement.service.IClearDayDetailService;
import com.yjd.comm.util.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 日结算明细
 * Created by yangchangyan on 2017/9/21.
 */
@Scope("prototype")
@Controller
@RequestMapping("/settlement/day/detail")
public class ClearDayDetailControl extends BaseControl {

    @Resource
    private IClearDayDetailService clearDayDetailService;

    @Resource
    private IOrganizationService organizationService;

    @Resource
    private IOrderService orderService;

    @RequestMapping(value = "/list")
    public String orderSettlement(String startDate, String endDate) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("sort", "clear_date desc");
        paramMap.put("rows", "20");

        if (StringUtil.isNotEmpty(startDate)) {
            paramMap.put("clear_date_gte", startDate.replace("-", ""));
        }
        if (StringUtil.isNotEmpty(endDate)) {
            paramMap.put("clear_date_lte", endDate.replace("-", ""));
        }

        PagerHtmlModel pager = PagerUtil.getPagerHtmlModel(
                this.clearDayDetailService.getPageModel(paramMap, DbWREnums.READ), paramMap,
                ServletUtil.getUrl("/settlement/day/detail/list"));

        Map<Long, String> orgIdAndName = organizationService.allOrgAndName();
        ClearDayDetailModel cddm = null;
        for (Object obj : pager.getPagerModel().getDataList()) {
            cddm = (ClearDayDetailModel)obj;
            cddm.getStrMap().put("channel_name",FundChannelEnum.parse(cddm.getFund_channel()).getDesc());
            cddm.getStrMap().put("status", StagesStatusEnum.parse(cddm.getStatus()).getLabel());
            cddm.getStrMap().put("org_name", orgIdAndName.get(cddm.getOrg_id()));
            cddm.getStrMap().put("create_time", DateUtil.timestampToDateString(cddm.getCreate_time(), DateUtil.PATTERN_yyyy_MM_dd_HH_mm_ss));
        }
        request.setAttribute("maps", paramMap);
        request.setAttribute("pager", pager);
        request.setAttribute("orgids", organizationService.allOrgAndName());
        request.setAttribute("fundChannelList", FundChannelEnum.getFundChannelList());
        return "day.detail.list";
    }

    /**
     * 生成日结算明细
     */
    @RequestMapping(value = "/generate")
    public String generateSettlement(String startDate, String endDate) throws Exception {

        Long startTime = FrameUtil.getTime();
        if(StringUtil.isNotEmpty(startDate)){
            startTime = DateUtil.dateStringToTimestamp(startDate, DateUtil.PATTERN_yyyy_MM_dd);
        }

        Long endTime = FrameUtil.getTime();
        if(StringUtil.isNotEmpty(endDate)){
            endTime = DateUtil.dateStringToTimestamp(endDate, DateUtil.PATTERN_yyyy_MM_dd);
        }

        if(startTime > endTime){
            FrameUtil.throwBaseException1("日期不合法");
        }

        orderService.settlement(startTime, endTime);

        return "redirect:/settlement/day/detail/list.action";
    }

}
